<?php

class Dich_vu extends Admin_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model("Quanly_m");
        $this->_data['sidebar'] = "dich_vu";
        //$this->load->library('formdate');
    }

    public function index() {
        $this->danh_sach();
    }

    //-- Them loai dich vu --//
    public function them_moi_dang_dich_vu() {
        is_admin();
        $this->_data['page_title'] = "thêm dạng dịch vụ";
// validate form config

        $config = array(
            array("field" => "ten_dich_vu",
                "label" => "Tên dạng dịch vụ",
                "rules" => "required|trim|xss_clean"
            )
        );
        $this->form_validation->set_rules($config); //set validation rules

        if ($this->input->post('submit')) {
            if ($this->form_validation->run() === TRUE) {

                $data_dichvu = array(
                    'ten_dich_vu' => $this->input->post('ten_dich_vu')
                );

                $query = $this->Quanly_m->insert_data('dang_dichvu', $data_dichvu);

                if ($query === TRUE) {
                    $this->session->set_flashdata('error', 'Thêm mới thành công');
                    redirect('dich_vu/dang_dich_vu');
                } else {
                    $this->session->set_flashdata('error', 'Thêm mới thất bại');
                    redirect('dich_vu/dang_dich_vu');
                }
            } else {
                $this->display_admin('admin/quanly/them_dang_dich_vu');
            }
        } else {
            $this->display_admin('admin/quanly/them_dang_dich_vu');
        }
    }

    //-- List lien he --//
    public function dang_dich_vu() {
        is_admin();
        $this->_data['page_title'] = "danh sach dạng dịch vụ";

        $perpage = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($perpage == "") {
            $perpage = 10; //10 records per page
        }
        if ($offset == "") {
            $offset = "";
        }
        $config['base_url'] = base_url() . "index.php/dich_vu/dang_dich_vu/" . $perpage . "/";
        $config['per_page'] = $perpage;
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Quanly_m->_get_service_type(0, 0, TRUE);
        if ($offset > 0 && $offset == $config['total_rows']) {
            $offset = $offset - $perpage;
        }
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $config['total_rows'];
        $this->_data['perpage'] = $perpage;
        $this->_data['offset'] = $offset;

        $query = $this->Quanly_m->_get_service_type($perpage, $offset, FALSE);
        $this->_data['dichvu_data'] = $query;
        $this->display_admin("admin/quanly/danh_sach_dang_dich_vu");
    }

    //-- Sua thong tin khach hang --//
    public function sua_thong_tin_dang_dich_vu($id) {
        is_admin();
        $this->_data['page_title'] = "Sửa thông tin dạng dịch vụ";
        $dichvu_data = $this->Quanly_m->_get_data_by_id("dang_dichvu", $id);
        $this->_data['data'] = $dichvu_data;
        if ($this->input->post('submit')) {
            $config = array(
                array("field" => "ten_dich_vu",
                    "label" => "Tên dạng dịch vụ",
                    "rules" => "required|trim|xss_clean"
                )
            );
            $this->form_validation->set_rules($config); //set validation rules

            if ($this->form_validation->run() === TRUE) {

                $data_dichvu = array(
                    'ten_dich_vu' => $this->input->post('ten_dich_vu')
                );

                $query = $this->Quanly_m->update_data('dang_dichvu', $data_dichvu, $id);
                if ($query === TRUE) {
                    $this->session->set_flashdata('error', 'Cập nhật thành công');
                    redirect('dich_vu/dang_dich_vu');
                } else {
                    $this->session->set_flashdata('error', 'Cập nhật thất bại');
                    redirect('dich_vu/dang_dich_vu');
                }
            }
        } else {
            $this->display_admin('admin/quanly/sua_dang_dich_vu');
        }
    }

    //-- Xoa thong tin dang dich vu --//
    public function xoa_thong_tin_dang_dich_vu($id) {
        if ($this->Quanly_m->delete_data('dang_dichvu', $id) === TRUE) {
            $this->session->set_flashdata('error', 'Xóa thành công');
            redirect('dich_vu/dang_dich_vu');
        } else {
            $this->session->set_flashdata('error', 'Xóa thất bại');
            redirect('dich_vu/dang_dich_vu');
        }
    }

    //-- Them Khach hang --//
    public function them_moi() {
        is_admin();
        $this->_data['page_title'] = "thêm dịch vụ";

        // validate form config

        $config = array(
            array("field" => "khach_hang",
                "label" => "khách hàng",
                "rules" => "trim|xss_clean"
            ),
            array("field" => "dang_dich_vu",
                "label" => "dạng dịch vụ",
                "rules" => "trim|xss_clean"
            ),
            array("field" => "ten_website",
                "label" => "Tên Website",
                "rules" => "required|trim|xss_clean"
            ),
            array("field" => "so_tien",
                "label" => "Số tiền",
                "rules" => "required|trim|xss_clean"
            )
        );
        $this->form_validation->set_rules($config); //set validation rules

        if ($this->input->post('submit')) {
            if ($this->form_validation->run() === TRUE) {
                $start_date = $this->input->post('start_year') . '-' . $this->input->post('start_month') . '-' . $this->input->post('start_date') . ' ' . date('H:i', time());
                $end_date = $this->input->post('end_year') . '-' . $this->input->post('end_month') . '-' . $this->input->post('end_date') . ' ' . date('H:i', time());
                $start_long = human_to_unix($start_date);
                $end_long = human_to_unix($end_date);
//                $now = time();
//
//                $human = unix_to_human($now);
//
//                $unix = human_to_unix($human);

                $data_dichvu = array(
                    'ngay_bat_dau' => $start_long,
                    'ngay_ket_thuc' => $end_long,
                    'id_khach_hang' => $this->input->post('khach_hang'),
                    'id_dang_dich_vu' => $this->input->post('dang_dich_vu'),
                    'ten_website' => $this->input->post('ten_website'),
                    'id_sale' => $this->session->userdata('admin_id')
                );

                $query = $this->Quanly_m->insert_data('dichvu', $data_dichvu);

                $id_dichvu = $this->db->insert_id();
                $data_thanhtoan = array(
                    'id_dich_vu' => $id_dichvu,
                    'so_tien' => $this->input->post('so_tien'),
                    'tinh_trang' => $this->input->post('tinh_trang')
                );
                $this->Quanly_m->insert_data('thanhtoan', $data_thanhtoan);

                if ($query === TRUE) {
                    $this->session->set_flashdata('error', 'Thêm mới thành công');
                    redirect('dich_vu');
                } else {
                    $this->session->set_flashdata('error', 'Thêm mới thất bại');
                    redirect('dich_vu');
                }
            } else {
                $this->display_admin('admin/quanly/them_dich_vu');
            }
        } else {
            $this->display_admin('admin/quanly/them_dich_vu');
        }
    }

    //-- List lien he --//
    public function danh_sach() {
        is_admin();
        $this->_data['page_title'] = "danh sach dich vu";
        $sale_id = $this->session->userdata('admin_id');
        $perpage = $this->uri->segment(3);
        $offset = $this->uri->segment(4);
        if ($perpage == "") {
            $perpage = 10; //10 records per page
        }
        if ($offset == "") {
            $offset = "";
        }
        $config['base_url'] = base_url() . "index.php/dich_vu/danh_sach/" . $perpage . "/";
        $config['per_page'] = $perpage;
        $config['uri_segment'] = 4;
        $config['total_rows'] = $this->Quanly_m->_get_service_by_sale_id($sale_id, 0, 0, TRUE);
        if ($offset > 0 && $offset == $config['total_rows']) {
            $offset = $offset - $perpage;
        }
        $this->pagination->initialize($config);
        $this->_data['pagination'] = $this->pagination->create_links();
        $this->_data['total'] = $config['total_rows'];
        $this->_data['perpage'] = $perpage;
        $this->_data['offset'] = $offset;

        $query = $this->Quanly_m->_get_service_by_sale_id($sale_id, $perpage, $offset, FALSE);
        $this->_data['dichvu_data'] = $query;
        $this->display_admin("admin/quanly/danh_sach_dich_vu");
    }

    //-- Sua thong tin khach hang --//
    public function sua_thong_tin($id) {
        is_admin();
        $this->_data['page_title'] = "Sửa thông tin dịch vụ";
        $dichvu_data = $this->Quanly_m->_get_data_by_id("dichvu", $id);
        $thanhtoan_data = $this->Quanly_m->_get_pay_infor($id);

        $this->_data['data'] = $dichvu_data;
        $this->_data['tinh_trang_thanh_toan'] = $thanhtoan_data->tinh_trang;
        if ($this->input->post('submit')) {
            $config = array(
                array("field" => "khach_hang",
                    "label" => "khách hàng",
                    "rules" => "trim|xss_clean"
                ),
                array("field" => "dang_dich_vu",
                    "label" => "dạng dịch vụ",
                    "rules" => "trim|xss_clean"
                ),
                array("field" => "ten_website",
                    "label" => "Tên Website",
                    "rules" => "required|trim|xss_clean"
                )
            );
            $this->form_validation->set_rules($config); //set validation rules

            if ($this->form_validation->run() === TRUE) {
                $start_date = $this->input->post('start_year') . '-' . $this->input->post('start_month') . '-' . $this->input->post('start_date') . ' ' . date('H:i', time());
                $end_date = $this->input->post('end_year') . '-' . $this->input->post('end_month') . '-' . $this->input->post('end_date') . ' ' . date('H:i', time());
                $start_long = human_to_unix($start_date);
                $end_long = human_to_unix($end_date);

                $data_dichvu = array(
                    'ngay_bat_dau' => $start_long,
                    'ngay_ket_thuc' => $end_long,
                    'id_khach_hang' => $this->input->post('khach_hang'),
                    'id_dang_dich_vu' => $this->input->post('dang_dich_vu'),
                    'ten_website' => $this->input->post('ten_website')
                );

                $query = $this->Quanly_m->update_data('dichvu', $data_dichvu, $id);
                $tinh_trang = 1;
                if ($this->input->post('tinh_trang') == '') {
                    $tinh_trang = 0;
                }
              
                $this->Quanly_m->update_data('thanhtoan', array('tinh_trang' => $tinh_trang), $thanhtoan_data->id);
                if ($query === TRUE) {

                    $this->session->set_flashdata('error', 'Cập nhật thành công');
                    redirect('dich_vu');
                } else {
                    $this->session->set_flashdata('error', 'Cập nhật thất bại');
                    redirect('dich_vu');
                }
            }
        } else {
            $this->display_admin('admin/quanly/sua_dich_vu');
        }
    }

    //-- Xoa thong tin khach hang --//
    public function xoa_thong_tin($id) {
        if ($this->Quanly_m->delete_data('dichvu', $id) === TRUE) {
            $this->session->set_flashdata('error', 'Xóa thành công');
            redirect('dich_vu');
        } else {
            $this->session->set_flashdata('error', 'Xóa thất bại');
            redirect('dich_vu');
        }
    }

    //-- Them Khach hang --//
    public function cap_nhat_thanh_toan($id) {
        is_admin();
        $this->_data['page_title'] = "cập nhật thanh toán";

        // validate form config

        $config = array(
            array("field" => "khach_hang",
                "label" => "khách hàng",
                "rules" => "trim|xss_clean"
            ),
            array("field" => "dang_dich_vu",
                "label" => "dạng dịch vụ",
                "rules" => "trim|xss_clean"
            )
        );
        $this->form_validation->set_rules($config); //set validation rules

        if ($this->input->post('submit')) {
            if ($this->form_validation->run() === TRUE) {
                $start_date = $this->input->post('start_year') . '-' . $this->input->post('start_month') . '-' . $this->input->post('start_date') . ' ' . date('H:i', time());
                $end_date = $this->input->post('end_year') . '-' . $this->input->post('end_month') . '-' . $this->input->post('end_date') . ' ' . date('H:i', time());
                $start_long = human_to_unix($start_date);
                $end_long = human_to_unix($end_date);
//                $now = time();
//
//                $human = unix_to_human($now);
//
//                $unix = human_to_unix($human);

                $data_dichvu = array(
                    'ngay_bat_dau' => $start_long,
                    'ngay_ket_thuc' => $end_long,
                    'id_khach_hang' => $this->input->post('khach_hang'),
                    'id_dang_dich_vu' => $this->input->post('dang_dich_vu'),
                    'id_sale' => $this->session->userdata('admin_id')
                );

                $query = $this->Quanly_m->insert_data('dichvu', $data_dichvu);

                if ($query === TRUE) {
                    $this->session->set_flashdata('error', 'Thêm mới thành công');
                    redirect('dich_vu');
                } else {
                    $this->session->set_flashdata('error', 'Thêm mới thất bại');
                    redirect('dich_vu');
                }
            } else {
                $this->display_admin('admin/quanly/sua_thanh_toan');
            }
        } else {
            $this->display_admin('admin/quanly/sua_thanh_toan');
        }
    }

}